﻿Imports System.Data.SqlClient

Public Class clsAdeudos
    Private mintIdAdeudo As Integer
    Private mstrNombre As String
    Private mintAplicacion As Integer
    Private mintDescuento As Integer
    Private mdblCantidad As Double
    Private mintPrioridad As Integer
    Private mintIdConcepto As Integer

    Public Property IDAdeudo() As Integer
        Get
            Return mintIdAdeudo
        End Get
        Set(ByVal value As Integer)
            mintIdAdeudo = value
        End Set
    End Property

    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(ByVal value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Aplicacion() As Integer
        Get
            Return mintAplicacion
        End Get
        Set(ByVal value As Integer)
            mintAplicacion = value
        End Set
    End Property
    Public Property Descuento() As Integer
        Get
            Return mintDescuento
        End Get
        Set(ByVal value As Integer)
            mintDescuento = value
        End Set
    End Property
    Public Property Cantidad() As Double
        Get
            Return mdblCantidad
        End Get
        Set(ByVal value As Double)
            mdblCantidad = value
        End Set
    End Property
    Public Property Prioridad() As Integer
        Get
            Return mintPrioridad
        End Get
        Set(ByVal value As Integer)
            mintPrioridad = value
        End Set
    End Property
    Public Property IDConcepto() As Integer
        Get
            Return mintIdConcepto
        End Get
        Set(ByVal value As Integer)
            mintIdConcepto = value
        End Set
    End Property
    Public Sub New()
        mintIdAdeudo = 0
        mstrNombre = ""
        mintAplicacion = 0
        mintDescuento = 0
        mdblCantidad = 0
        mintPrioridad = 0
        mintIdConcepto = 0
    End Sub
    Public Sub Guardar()

        If IDAdeudo = 0 Then
            Exit Sub
        End If

        Dim intRetVal As Integer

        intRetVal = BuscarIntDatoCitra("Adeudos", "IDAdeudo", "IDAdeudo =" & IDAdeudo)

        If intRetVal = 0 Then
            Insertar()
        Else
            Actualizar()
        End If

    End Sub
    Public Sub Borrar()
        Call SetSqlCitra("Delete From MGWADEUDOS Where cIDAdeudo = " & IDAdeudo)
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From ADEUDOS " & _
        " WHERE IDAdeudo=" & IDAdeudo

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mstrNombre = rdBuscar("Nombre").ToString.Trim
            mintAplicacion = rdBuscar("Aplicacion")
            mintDescuento = rdBuscar("Descuento")
            mdblCantidad = rdBuscar("Cantidad")
            mintPrioridad = rdBuscar("Prioridad")
            mintIdConcepto = rdBuscar("idconcepto")
        Else
            mintIdAdeudo = 0
            mstrNombre = ""
            mintAplicacion = 0
            mintDescuento = 0
            mdblCantidad = 0
            mintPrioridad = 0
            mintIdConcepto = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Adeudos (" & _
                            "IdAdeudo," & _
                            "Nombre," & _
                            "Aplicacion," & _
                            "Descuento," & _
                            "Cantidad," & _
                            "Prioridad," & _
                            "IdConcepto)" & _
                            "VALUES (" & _
                            "@IdAdeudo," & _
                            "@Nombre," & _
                            "@Aplicacion," & _
                            "@Descuento," & _
                            "@Cantidad," & _
                            "@Prioridad," & _
                            "@IdConcepto)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAdeudo", mintIdAdeudo)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Aplicacion", mintAplicacion)
        Command.Parameters.AddWithValue("Descuento", mintDescuento)
        Command.Parameters.AddWithValue("Cantidad", mdblCantidad)
        Command.Parameters.AddWithValue("Prioridad", mintPrioridad)
        Command.Parameters.AddWithValue("IdConcepto", mintIdConcepto)


        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update Adeudos Set " & _
                    "Nombre=@Nombre," & _
                    "Aplicacion=@Aplicacion," & _
                    "Descuento=@Descuento," & _
                    "Cantidad=@Cantidad," & _
                    "Prioridad=@Prioridad," & _
                    "IdConcepto=@IdConcepto " & _
                    "Where IdAdeudo=@IdAdeudo"


        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("IdAdeudo", mintIdAdeudo)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Aplicacion", mintAplicacion)
        Command.Parameters.AddWithValue("Descuento", mintDescuento)
        Command.Parameters.AddWithValue("Cantidad", mdblCantidad)
        Command.Parameters.AddWithValue("Prioridad", mintPrioridad)
        Command.Parameters.AddWithValue("IdConcepto", mintIdConcepto)

        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el registro ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
